import traceback
from selenium.webdriver.support.ui import WebDriverWait
import time

class Base():

    # 封装driver方法
    def __init__(self, driver):
        self.driver = driver

    # 封装打开浏览器操作
    def open_url(self, url):
        self.driver.get(url)

    def screenshot(self, name):
        t = time.strftime('%Y%m%d%H%M%S', time.localtime())
        self.driver.save_screenshot(t+name)

    # 查找页面元素
    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.screenshot('A3mall.png')
            print(traceback.format_exc())

    # 封装点击页面元素操作
    def click(self, loc):
        self.find_element(loc).click()

    # 封装输入操作
    def send_keys(self, loc, text):
        self.find_element(loc).send_keys(text)